package com.ruoyi.imports.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.imports.domain.OperatingExcelData;
import com.ruoyi.imports.domain.ProcessExcelData;
import com.ruoyi.imports.listener.OperatingExcelDataListener;
import com.ruoyi.imports.listener.ProcessExcelDataListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.imports.domain.TdOperatingParamsType;
import com.ruoyi.imports.service.ITdOperatingParamsTypeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工艺参数类型Controller
 * 
 * @author ruoyi
 * @date 2025-07-14
 */
@RestController
@RequestMapping("/paramsType")
public class TdOperatingParamsTypeController extends BaseController
{
    @Autowired
    private ITdOperatingParamsTypeService tdOperatingParamsTypeService;

    /**
     * 查询工艺参数类型列表
     */
    @RequiresPermissions("imports:type:list")
    @GetMapping("/list")
    public TableDataInfo list(TdOperatingParamsType tdOperatingParamsType)
    {
        startPage();
        List<TdOperatingParamsType> list = tdOperatingParamsTypeService.selectTdOperatingParamsTypeList(tdOperatingParamsType);
        return getDataTable(list);
    }

    /**
     * 导出工艺参数类型列表
     */
    @RequiresPermissions("imports:type:export")
    @Log(title = "工艺参数类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdOperatingParamsType tdOperatingParamsType)
    {
        List<TdOperatingParamsType> list = tdOperatingParamsTypeService.selectTdOperatingParamsTypeList(tdOperatingParamsType);
        ExcelUtil<TdOperatingParamsType> util = new ExcelUtil<TdOperatingParamsType>(TdOperatingParamsType.class);
        util.exportExcel(response, list, "工艺参数类型数据");
    }

    /**
     * 获取工艺参数类型详细信息
     */
    @RequiresPermissions("imports:type:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tdOperatingParamsTypeService.selectTdOperatingParamsTypeById(id));
    }

    /**
     * 新增工艺参数类型
     */
    @RequiresPermissions("imports:type:add")
    @Log(title = "工艺参数类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdOperatingParamsType tdOperatingParamsType)
    {
        return toAjax(tdOperatingParamsTypeService.insertTdOperatingParamsType(tdOperatingParamsType));
    }

    /**
     * 修改工艺参数类型
     */
    @RequiresPermissions("imports:type:edit")
    @Log(title = "工艺参数类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdOperatingParamsType tdOperatingParamsType)
    {
        return toAjax(tdOperatingParamsTypeService.updateTdOperatingParamsType(tdOperatingParamsType));
    }

    /**
     * 删除工艺参数类型
     */
    @RequiresPermissions("imports:type:remove")
    @Log(title = "工艺参数类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tdOperatingParamsTypeService.deleteTdOperatingParamsTypeByIds(ids));
    }


    //    @RequiresPermissions("imports:imports:import")
    @Log(title = "过程数据监测表", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file,
                                  @RequestParam(value = "projectId", required = false) Long projectId) {
        try {
            // 1. 创建监听器
            OperatingExcelDataListener listener = new OperatingExcelDataListener();

            // 2. 使用监听器读取 Excel 数据
            EasyExcel.read(file.getInputStream(), OperatingExcelData.class, listener)
                    .headRowNumber(4)
                    .sheet(2) // 读取第一个 sheet
                    .doRead(); // 执行读取

            // 3. 从监听器获取解析后的数据
            List<OperatingExcelData> excelDataList = listener.getDataList();

            // 4. 检查数据是否为空
            if (excelDataList == null || excelDataList.isEmpty()) {
                return error("Excel 文件中未找到有效数据");
            }
            // 5. 调用 Service 处理导入逻辑
            tdOperatingParamsTypeService.importOperatingDataType(excelDataList, projectId);

            return success("导入成功，共导入 " + excelDataList.size() + " 条数据");

        } catch (IOException e) {
            return error("导入失败：文件读取异常，" + e.getMessage());
        } catch (Exception e) {
            return error("导入失败：" + e.getMessage());
        }
    }
}
